package com.spark.lola.service.sup.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.pur.OrderInfo;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.feign.SupFeignService;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.service.pur.IOrderInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.configuration.WmsProperties;
import com.spark.lola.support.wms.constant.SupOrderByType;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author: DengYeJun
 * @Date: 2020/4/22 14:47
 */
public class SupFeignServiceImpl implements SupFeignService {
    private SupplierInfoMapper supplierInfoMapper;
    private IOrderInfoService orderInfoService;
    private WmsProperties wmsProperties;
    public SupFeignServiceImpl(SupplierInfoMapper supplierInfoMapper,IOrderInfoService orderInfoService,WmsProperties wmsProperties){
        this.supplierInfoMapper = supplierInfoMapper;
        this.orderInfoService = orderInfoService;
        this.wmsProperties = wmsProperties;
    }
    @Override
    public SupplierInfo findSupplierById(Long id) {
        return supplierInfoMapper.selectById(id);
    }

    @Override
    public OrderInfo findOrderByOrderNo(String orderNo) {
        return orderInfoService.findFullByNo(orderNo);

    }

    @Override
    public OrderInfo findOrderByOrderId(Long id) {
        return orderInfoService.findFullById(id);
    }

    @Override
    public List<SupplierInfo> supliers() {
        QueryWrapper<SupplierInfo> query =new QueryWrapper<>();
        query.eq("used", CommonConstant.Y);
        return supplierInfoMapper.selectList(query);
    }

    @Override
    public Map<String,BigDecimal> statistics(Long supplierId,Long salesmanId, Long destinationId, String start, String end) {
        int type =0 ;
        Long parentId = supplierId;
        if(salesmanId!=null){
            type =1;
            parentId = salesmanId;
        }
        if(destinationId!=null){
            type =4;
            parentId = destinationId;
        }
        return orderInfoService.statistics(type,parentId,start,end);
    }

    @Override
    public BigDecimal waitingPayment(Integer type, Long byId,List<String> salesmanIds,String start, String end) {
        return orderInfoService.waitingPayment(type,byId,salesmanIds,start,end);
    }

    @Override
    public BigDecimal supplierWaitingPayment(Long supplierId) {
        return orderInfoService.supplierWaitingPayment(supplierId);
    }

    @Override
    public Integer supplierCount(Long salesmanId) {
        QueryWrapper<SupplierInfo> query =new QueryWrapper<>();
        query.eq("used",CommonConstant.Y);
        if(!salesmanId.equals(0L)){
            query.eq("salesman_id",salesmanId);
        }
        return supplierInfoMapper.selectCount(query);
    }

    @Override
    public Integer salesmanCount() {
        return CollectionCache.dutyEmployees.get(wmsProperties.getPurchase().getDutyId().get(1)).size();
    }

    @Override
    public Map<Integer, Integer> orderCountByStatus(Integer type, Long byId,List<String> salesmanIds) {
        return orderInfoService.orderCountByStatus(type,byId,salesmanIds);
    }
}
